home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / firefox-3.5.5 / components / nsSessionStartup.js < prev    next >
Text File  |  2009-11-09  |  9KB  |  263 lines

  1. /* 
  2. //@line 38 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/sessionstore/src/nsSessionStartup.js"
  3. */
  4.  
  5. /**
  6. //@line 65 "/build/buildd/firefox-3.5-3.5.5+nobinonly/build-tree/mozilla/browser/components/sessionstore/src/nsSessionStartup.js"
  7. */
  8.  
  9. /* :::::::: Constants and Helpers ::::::::::::::: */
  10.  
  11. const Cc = Components.classes;
  12. const Ci = Components.interfaces;
  13. const Cr = Components.results;
  14. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  15.  
  16. const STATE_RUNNING_STR = "running";
  17.  
  18. function debug(aMsg) {
  19.   aMsg = ("SessionStartup: " + aMsg).replace(/\S{80}/g, "$&\n");
  20.   Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService)
  21.                                      .logStringMessage(aMsg);
  22. }
  23.  
  24. /* :::::::: The Service ::::::::::::::: */
  25.  
  26. function SessionStartup() {
  27. }
  28.  
  29. SessionStartup.prototype = {
  30.  
  31.   // the state to restore at startup
  32.   _iniString: null,
  33.   _sessionType: Ci.nsISessionStartup.NO_SESSION,
  34.  
  35. /* ........ Global Event Handlers .............. */
  36.  
  37.   /**
  38.    * Initialize the component
  39.    */
  40.   init: function sss_init() {
  41.     // do not need to initialize anything in auto-started private browsing sessions
  42.     let pbs = Cc["@mozilla.org/privatebrowsing;1"].
  43.               getService(Ci.nsIPrivateBrowsingService);
  44.     if (pbs.autoStarted)
  45.       return;
  46.  
  47.     let prefBranch = Cc["@mozilla.org/preferences-service;1"].
  48.                      getService(Ci.nsIPrefService).getBranch("browser.");
  49.  
  50.     // get file references
  51.     var dirService = Cc["@mozilla.org/file/directory_service;1"].
  52.                      getService(Ci.nsIProperties);
  53.     let sessionFile = dirService.get("ProfD", Ci.nsILocalFile);
  54.     sessionFile.append("sessionstore.js");
  55.     
  56.     let doResumeSession = prefBranch.getBoolPref("sessionstore.resume_session_once") ||
  57.                           prefBranch.getIntPref("startup.page") == 3;
  58.     
  59.     // only read the session file if config allows possibility of restoring
  60.     var resumeFromCrash = prefBranch.getBoolPref("sessionstore.resume_from_crash");
  61.     if (!resumeFromCrash && !doResumeSession || !sessionFile.exists())
  62.       return;
  63.     
  64.     // get string containing session state
  65.     this._iniString = this._readStateFile(sessionFile);
  66.     if (!this._iniString)
  67.       return;
  68.     
  69.     try {
  70.       // parse the session state into JS objects
  71.       var s = new Components.utils.Sandbox("about:blank");
  72.       var initialState = Components.utils.evalInSandbox("(" + this._iniString + ")", s);
  73.     }
  74.     catch (ex) { debug("The session file is invalid: " + ex); } 
  75.     
  76.     let lastSessionCrashed =
  77.       initialState && initialState.session && initialState.session.state &&
  78.       initialState.session.state == STATE_RUNNING_STR;
  79.     
  80.     // set the startup type
  81.     if (lastSessionCrashed && resumeFromCrash)
  82.       this._sessionType = Ci.nsISessionStartup.RECOVER_SESSION;
  83.     else if (!lastSessionCrashed && doResumeSession)
  84.       this._sessionType = Ci.nsISessionStartup.RESUME_SESSION;
  85.     else
  86.       this._iniString = null; // reset the state string
  87.  
  88.     if (this._sessionType != Ci.nsISessionStartup.NO_SESSION) {
  89.       // wait for the first browser window to open
  90.       var observerService = Cc["@mozilla.org/observer-service;1"].
  91.                             getService(Ci.nsIObserverService);
  92.       observerService.addObserver(this, "domwindowopened", true);
  93.       observerService.addObserver(this, "browser:purge-session-history", true);
  94.     }
  95.   },
  96.  
  97.   /**
  98.    * Handle notifications
  99.    */
  100.   observe: function sss_observe(aSubject, aTopic, aData) {
  101.     var observerService = Cc["@mozilla.org/observer-service;1"].
  102.                           getService(Ci.nsIObserverService);
  103.  
  104.     switch (aTopic) {
  105.     case "app-startup": 
  106.       observerService.addObserver(this, "final-ui-startup", true);
  107.       observerService.addObserver(this, "quit-application", true);
  108.       break;
  109.     case "final-ui-startup": 
  110.       observerService.removeObserver(this, "final-ui-startup");
  111.       observerService.removeObserver(this, "quit-application");
  112.       this.init();
  113.       break;
  114.     case "quit-application":
  115.       // no reason for initializing at this point (cf. bug 409115)
  116.       observerService.removeObserver(this, "final-ui-startup");
  117.       observerService.removeObserver(this, "quit-application");
  118.       break;
  119.     case "domwindowopened":
  120.       var window = aSubject;
  121.       var self = this;
  122.       window.addEventListener("load", function() {
  123.         self._onWindowOpened(window);
  124.         window.removeEventListener("load", arguments.callee, false);
  125.       }, false);
  126.       break;
  127.     case "browser:purge-session-history":
  128.       // reset all state on sanitization
  129.       this._iniString = null;
  130.       this._sessionType = Ci.nsISessionStartup.NO_SESSION;
  131.       // no need in repeating this, since startup state won't change
  132.       observerService.removeObserver(this, "browser:purge-session-history");
  133.       break;
  134.     }
  135.   },
  136.  
  137.   /**
  138.    * Removes the default arguments from the first browser window
  139.    * (and removes the "domwindowopened" observer afterwards).
  140.    */
  141.   _onWindowOpened: function sss_onWindowOpened(aWindow) {
  142.     var wType = aWindow.document.documentElement.getAttribute("windowtype");
  143.     if (wType != "navigator:browser")
  144.       return;
  145.     
  146.     /**
  147.      * Note: this relies on the fact that nsBrowserContentHandler will return
  148.      * a different value the first time its getter is called after an update,
  149.      * due to its needHomePageOverride() logic. We don't want to remove the
  150.      * default arguments in the update case, since they include the "What's
  151.      * New" page.
  152.      *
  153.      * Since we're garanteed to be at least the second caller of defaultArgs
  154.      * (nsBrowserContentHandler calls it to determine which arguments to pass
  155.      * at startup), we know that if the window's arguments don't match the
  156.      * current defaultArguments, we're either in the update case, or we're
  157.      * launching a non-default browser window, so we shouldn't remove the
  158.      * window's arguments.
  159.      */
  160.     var defaultArgs = Cc["@mozilla.org/browser/clh;1"].
  161.                       getService(Ci.nsIBrowserHandler).defaultArgs;
  162.     if (aWindow.arguments && aWindow.arguments[0] &&
  163.         aWindow.arguments[0] == defaultArgs)
  164.       aWindow.arguments[0] = null;
  165.     
  166.     var observerService = Cc["@mozilla.org/observer-service;1"].
  167.                           getService(Ci.nsIObserverService);
  168.     observerService.removeObserver(this, "domwindowopened");
  169.   },
  170.  
  171. /* ........ Public API ................*/
  172.  
  173.   /**
  174.    * Get the session state as a string
  175.    */
  176.   get state() {
  177.     return this._iniString;
  178.   },
  179.  
  180.   /**
  181.    * Determine whether there is a pending session restore.
  182.    * @returns bool
  183.    */
  184.   doRestore: function sss_doRestore() {
  185.     return this._sessionType != Ci.nsISessionStartup.NO_SESSION;
  186.   },
  187.  
  188.   /**
  189.    * Get the type of pending session store, if any.
  190.    */
  191.   get sessionType() {
  192.     return this._sessionType;
  193.   },
  194.  
  195. /* ........ Storage API .............. */
  196.  
  197.   /**
  198.    * Reads a session state file into a string and lets
  199.    * observers modify the state before it's being used
  200.    *
  201.    * @param aFile is any nsIFile
  202.    * @returns a session state string
  203.    */
  204.   _readStateFile: function sss_readStateFile(aFile) {
  205.     var stateString = Cc["@mozilla.org/supports-string;1"].
  206.                         createInstance(Ci.nsISupportsString);
  207.     stateString.data = this._readFile(aFile) || "";
  208.     
  209.     var observerService = Cc["@mozilla.org/observer-service;1"].
  210.                           getService(Ci.nsIObserverService);
  211.     observerService.notifyObservers(stateString, "sessionstore-state-read", "");
  212.     
  213.     return stateString.data;
  214.   },
  215.  
  216.   /**
  217.    * reads a file into a string
  218.    * @param aFile
  219.    *        nsIFile
  220.    * @returns string
  221.    */
  222.   _readFile: function sss_readFile(aFile) {
  223.     try {
  224.       var stream = Cc["@mozilla.org/network/file-input-stream;1"].
  225.                    createInstance(Ci.nsIFileInputStream);
  226.       stream.init(aFile, 0x01, 0, 0);
  227.       var cvstream = Cc["@mozilla.org/intl/converter-input-stream;1"].
  228.                      createInstance(Ci.nsIConverterInputStream);
  229.       cvstream.init(stream, "UTF-8", 1024, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
  230.       
  231.       var content = "";
  232.       var data = {};
  233.       while (cvstream.readString(4096, data)) {
  234.         content += data.value;
  235.       }
  236.       cvstream.close();
  237.       
  238.       return content.replace(/\r\n?/g, "\n");
  239.     }
  240.     catch (ex) { Components.utils.reportError(ex); }
  241.     
  242.     return null;
  243.   },
  244.  
  245.   /* ........ QueryInterface .............. */
  246.   QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver,
  247.                                           Ci.nsISupportsWeakReference,
  248.                                           Ci.nsISessionStartup]),
  249.   classDescription: "Browser Session Startup Service",
  250.   classID:          Components.ID("{ec7a6c20-e081-11da-8ad9-0800200c9a66}"),
  251.   contractID:       "@mozilla.org/browser/sessionstartup;1",
  252.  
  253.   // get this contractID registered for certain categories via XPCOMUtils
  254.   _xpcom_categories: [
  255.     // make ourselves a startup observer
  256.     { category: "app-startup", service: true }
  257.   ]
  258.  
  259. };
  260.  
  261. function NSGetModule(aCompMgr, aFileSpec)
  262.   XPCOMUtils.generateModule([SessionStartup]);
  263.